package com.feige.controller;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.feige.constant.Constants;
import com.feige.pojo.AskForLeave;
import com.feige.pojo.dto.AskForLeaveFormDto;
import com.feige.pojo.vo.AskForLeaveDetailVo;
import com.feige.pojo.vo.AskForLeaveVo;
import com.feige.service.AskForLeaveService;
import com.feige.result.AjaxResult;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.BeanUtils;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;

/**
 * <p>
 * 请假表 前端控制器
 * </p>
 *
 * @author feige
 * @since 2020-11-20
 */
@Api(tags = "请假控制器")
@RestController
@RequestMapping("/sign/ask-for-leave")
public class AskForLeaveController {

    @Resource
    private AskForLeaveService askForLeaveService;


    @ApiOperation(value = "通过用户ID分页查询请假信息")
    @GetMapping("/get/{pageNum}/{userId}")
    public AjaxResult getAskForLeaveById(@PathVariable String userId, @PathVariable Integer pageNum){
        List<AskForLeaveVo> leaves = askForLeaveService.getAskForLeaveById(pageNum, Constants.PAGE_SIZE,userId);
        long total = askForLeaveService.getAskForLeaveCountById(userId);
        AjaxResult result = AjaxResult.success(leaves);
        result.put("total",total);
        return result;
    }

    @ApiOperation(value = "通过班级ID分页查询该班级的请假信息")
    @GetMapping("/audit/{pageNum}/{clazzId}")
    public AjaxResult getAskForLeaveByClazzId(@PathVariable String clazzId, @PathVariable Integer pageNum){
        List<AskForLeaveVo> leaves = askForLeaveService.getAskForLeaveByClazzId(pageNum, Constants.PAGE_SIZE,clazzId);
        long total = askForLeaveService.getAskForLeaveCountByClazzId(clazzId);
        AjaxResult result = AjaxResult.success(leaves);
        result.put("total",total);
        return result;
    }


    @ApiOperation(value = "保存请假信息")
    @PostMapping("/save")
    public AjaxResult save(@Validated @RequestBody AskForLeaveFormDto askForLeaveFormDto){
        AskForLeave askForLeave = new AskForLeave();
        BeanUtils.copyProperties(askForLeaveFormDto,askForLeave);
        boolean save = askForLeaveService.save(askForLeave);
        if (save){
            return AjaxResult.success();
        }else {
            return AjaxResult.error();
        }
    }

    @ApiOperation(value = "通过ID查询请假信息")
    @GetMapping("/get/{id}")
    public AjaxResult getAskForLeaveDetailById(@PathVariable String id,@RequestParam(required = false) String audit){
        AskForLeaveDetailVo leaves = askForLeaveService.getAskForLeaveDetailById(id,audit);
        return AjaxResult.success(leaves);
    }

    @ApiOperation(value = "通过ID更新请假信息")
    @PutMapping("/update")
    public AjaxResult update(@RequestBody AskForLeave askForLeave){
        return askForLeaveService.updateAskForLeave(askForLeave);
    }

    @ApiOperation(value = "通过用户ID查询是否有未销假信息")
    @GetMapping("/leave-off/{userId}")
    public AjaxResult getNotLeaveOffByUserId(@PathVariable String userId){
        QueryWrapper<AskForLeave> wrapper = new QueryWrapper<>();
        wrapper
                .eq("leave_user_id",userId)
                .eq("audit",2)
                .eq("is_delete",0)
                .eq("leave_off",0);
        int count = askForLeaveService.count(wrapper);
        if (count <= 0){
            QueryWrapper<AskForLeave> w = new QueryWrapper<>();
            w
                    .eq("leave_user_id",userId)
                    .eq("audit",1)
                    .eq("is_delete",0)
                    .eq("leave_off",0);
            int c = askForLeaveService.count(w);
            if (c <= 0){
                return AjaxResult.success();
            }else {
                return AjaxResult.error(5000,"您有未销假的请假信息，请先销假！");
            }
        }else {
            return AjaxResult.error(5000,"您有未审核的请假信息，请先通知班主任审核或撤销！");
        }

    }

}

